<?php

/*
*
* BEGIN scorecardspoller.php
*
*
* Dazzler Server Client Communication Interface - Score Card Poller
* @Description: This subroutine pushes data to the client when new data is avaialable
* @Authors: Salman Noor, Wits University
* @Copyright: © Wits University 2011 - All Rights Reserved
* @License: BSD License (http://www.linfo.org/bsdlicense.html)
*
*
* BEGIN CHANGELOG
* Developers working on thee files are REQUIRED to maintain this CHANGELOG.
* Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
* !==================================================================================================================================================================
*
* VERSION 1.0.0
*	+ File Created, Salman, 14 October 2011
*
* ==================================================================================================================================================================!
* END CHANGELOG
*/


/*******************************************************************************************************************************************************************
*
* BEGIN Direct Call Check
*
/******************************************************************************************************************************************************************/
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1)
{
	/* Read the ini language file */
	$ini_lang_array = array();
	$clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	$lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
	die ($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}
/*******************************************************************************************************************************************************************
*
* END Direct Call Check
*
/******************************************************************************************************************************************************************/

$lastmodif    = isset($_REQUEST['scoreCardStatsTimeStamp']) ? $_REQUEST['scoreCardStatsTimeStamp'] : -1;
$GameID = isset($_REQUEST['GameID']) ? $_REQUEST['GameID'] : null;
$Innings = isset($_REQUEST['innings']) ? $_REQUEST['innings'] : null;

$StatsAdmin = new StatsAdmin();
if ($GameID != NULL || $Innings != NULL)
{
  $currentmodif =  $StatsAdmin->getLastBallId($GameID);
}
else
{
       trigger_error("Long Polling parameters are not set properly. Game ID or Innings or both are not set", E_USER_ERROR);
}


while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
  usleep(10000); // sleep 10ms to unload the CPU
  clearstatcache();

    $currentmodif =  $StatsAdmin->getLastBallId($GameID);
}
$full_scorcard_ui = new full_scorecard_ui($GameID, $Innings);
// return a json array
$response = array();

$fallofWickets = $full_scorcard_ui->showFallofWickets();
$battingScoreCard = $full_scorcard_ui->showFullBattingScoreCard();
$bowlingScoreCard = $full_scorcard_ui->showFullBowlingScoreCard();
$response['battingScoreCard']       = $battingScoreCard;
$response['bowlingScoreCard']       = $bowlingScoreCard;
$response['fallofwickets']       = $fallofWickets;
$response['newTimeStamp'] = $currentmodif;
echo json_encode($response);

flush();

?>

